<?php
  function announcements_form_validation($form) {

   	distvalidation($form['field_anndist']['und']['#value']);



    /*Announcement Title*/
    maxlength_validation($form['title']['#value'], 255, $form['title']['#title'], 'title');
    minilength_validation($form['title']['#value'], 5, $form['title']['#title'], 'title');
    chkbadchar($form['title']['#value'], $form['title']['#title'],'title');



    chkbadchar($form['field_anntothers']['und'][0]['value']['#value'], $form['field_anntothers']['und'][0]['value']['#title'], 'field_anntothers');

    /*Announcement Keywords */
    minilength_validation($form['field_annkword']['und'][0]['value']['#value'], 5, $form['field_annkword']['und'][0]['value']['#title'], 'field_annkword');
    chkbadchar($form['field_annkword']['und'][0]['value']['#value'], $form['field_annkword']['und'][0]['value']['#title'], 'field_annkword');


	/*Announcement Description */
  	minilength_validation($form['field_anndesc']['und'][0]['value']['#value'], 5, $form['field_anndesc']['und'][0]['value']['#title'],'field_anndesc');
	chkbadchar($form['field_anndesc']['und'][0]['value']['#value'], $form['field_anndesc']['und'][0]['value']['#title'], 'field_anndesc');

    url_validation($form['field_annurl']['und'][0]['value']['#value']);

    $form_type = $form['field_annavail'];
	//echo"s". $form_type['value']['#value'];
	if ($form_type['value']['#value'] == 'OnlineAvailable') {

    $reference_url = $form['field_annurl'][0]['value']['#value'];
	//$reference_url = url_validation($reference_url);

    if(isURL($reference_url) == 0 && $reference_url != '') {
      form_set_error('field_annurl', t('Not a valid URL given in %name field', array(
                                                   '%name' => $form['field_annurl']['und'][0]['value']['#value']['#title'])));
    }
    elseif ($reference_url == '') {
      form_set_error('field_annurl', t(' %name field is required', array(
                                                   '%name' => $form['field_annurl']['und'][0]['value']['#value']['#title'])));
    }
  }
  if ($form_type['value']['#value'] == 'Download') {
    // Validating the file size
    $field_obj = $form['field_act_file_upload'];
	$file_allowed_obj = $form['field_form_allowedfiletype'];
    file_upload_validation($field_obj, $file_allowed_obj, 'field_act_file_upload');
  }

    /*
    ** Annauncement date validation
    */
    Commencement_date($form['field_annstart']['und'][0]['value']['date']['#value'],$form['field_annstart']['und'][0]['#title'],'field_annstart');

    $introduced_on = $form['field_annstart'][0]['value']['date']['#value'];
    $valid_upto = $form['scheduler_settings']['unpublish_on']['date']['#value'];

   /* $flag = 0;
    $flag += check_date_expire($form['field_annstart']);
    $flag += check_date_expire($form['field_annexpire']);
  */

    if ($introduced_on != "" && $valid_upto != "" && dateDiff("-", $valid_upto, $introduced_on) < 0 && $flag == 0) {
      form_set_error('field_annstart', t('%expire date is lesser than %start date', array('%expire' => 'Valid upto',
                                                                                    '%start' => $form['field_annstart']['#title'])));
      form_set_error('scheduler_settings',' ');
    }


  }

  function announcements_presave_validation($node) {
    $reference_url = $node->field_annurl[0]['value'];
    $node->field_annurl[0]['value'] = url_validation($reference_url);
  } // function form_presave_validation()

